﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Sprout.Repository.Template.XParseEngine
{
    public abstract class BaseDeSerializer
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="sqlBuffer"></param>
        /// <param name="conditionList"></param>
        /// <returns></returns>
        public virtual bool Deserialize(XmlNode node, StringBuilder sqlBuffer, Dictionary<string, string> conditionList)
        {
            string prepend = string.Empty;
            string property = string.Empty;
            string sqlPart = string.Empty;
            string compareValue = string.Empty;
            if (node.Attributes["Property"] != null)
            {
                property = node.Attributes["Property"].Value;
            }
            if (!conditionList.ContainsKey(property))
            {
                //throw new Exception($"未传入{property}参数");
                return false;
            }
            if (node.Attributes["Prepend"] != null)
            {
                prepend = node.Attributes["Prepend"].Value.ToLower();
            }

            if (node.Attributes["CompareValue"] != null)
            {
                compareValue = node.Attributes["CompareValue"].Value;
            }
            string value = conditionList[property];
            if (ValidateProperty(node, prepend, property, value, compareValue))
            {
                if (node.ChildNodes.Count == 1)
                {
                    if (node.ChildNodes[0].NodeType == XmlNodeType.CDATA || node.ChildNodes[0].NodeType == XmlNodeType.Text)
                    {
                        sqlPart = " " + node.InnerText.Replace('\n', ' ').Replace('\r', ' ').Replace('\t', ' ').Trim();
                        if (sqlBuffer.ToString().ToLower().EndsWith("where"))
                        {
                            prepend = "";
                        }
                        if (!string.IsNullOrEmpty(prepend))
                        {
                            sqlPart = " " + prepend + sqlPart;
                        }
                        sqlPart = sqlPart.Replace("$" + property + "$", value);
                    }
                }
                else
                {
                    if (node.ChildNodes[0].NodeType == XmlNodeType.CDATA || node.ChildNodes[0].NodeType == XmlNodeType.Text)
                    {
                        sqlPart = " " + prepend;
                    }
                }

                sqlBuffer.Append(sqlPart);

                return true;
            }

            return false;
        }
        /// <summary>
        /// 验证节点
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="prepend">拼接字符串</param>
        /// <param name="property">属性</param>
        /// <param name="value">参数值</param>
        /// <param name="compareValue">节点判断值</param>
        /// <returns>是否成功</returns>
        public abstract bool ValidateProperty(XmlNode node, string prepend, string property, string value, string compareValue);


    }
}
